from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.utils import platform

if platform() == 'android':
    from jnius import autoclass
    from jnius import cast


class GoblinScreen(Screen):
    def __init__(self, **kwargs):
        self.register_event_type('on_back_pressed')
        self.register_event_type('on_comment')
        self.register_event_type('on_feed_refreshing')
        self.register_event_type('on_feed_refreshed')
        self.register_event_type('on_pull_to_refresh')
        self.register_event_type('on_no_service')

        super(GoblinScreen, self).__init__(**kwargs)

    def on_back_pressed(self, *args):
        print ('stopping app..')
        if platform == 'android':
            PythonActivity = autoclass('org.renpy.android.PythonActivity')
            Intent = autoclass('android.content.Intent')
            intent = Intent()
            intent.setAction(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_HOME)
            currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
            currentActivity.startActivity(intent)
        else:
            App.get_running_app().stop()

    def on_comment(self, *largs):
        return True

    def on_feed_refreshing(self, *args):
        return True

    def on_feed_refreshed(self, *args):
        return True
    
    def on_pull_to_refresh(self, *args):
        return True

    def on_no_service(self, *args):
        return True
